package com.wzs.rpc.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.dialect.Props;

public class ConfigUtils {

    public static <T> T loadConfig(Class<T> clz, String prefix) {
        return loadConfig(clz, prefix, "");
    }

    public static <T> T loadConfig(Class<T> clz, String prefix, String environment) {
        StringBuilder configFileBuilder = new StringBuilder("application");
        if(StrUtil.isNotBlank(environment)) {
            configFileBuilder.append("-").append(environment);
        }
        configFileBuilder.append(".properties");
        //自动从classpath加载配置文件
        Props props = new Props(configFileBuilder.toString());
        //将配置文件中以prefix为前缀的属性映射到clz的字段中
        return props.toBean(clz, prefix);
    }
}
